package com.systop.component;

import com.systop.utils.HttpUtil;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jsj
 * @date 2022/6/9
 */

@Data
@Component
public class SmsComponent {
    //配置常规参数
    @Value("${thridserve.sms.host}")
    private String host;
    @Value("${thridserve.sms.path}")
    private String path;
    @Value("${thridserve.sms.method}")
    private String method;
    @Value("${thridserve.sms.appcode}")
    private String appcode;
    @Value("${thridserve.sms.sign}")
    private String sign;
    @Value("${thridserve.sms.template}")
    private String template;

    public void sendSmsCode(String mobile,String code){
        System.out.println("当前："+mobile+"----->"+code);
        //配置header
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);

        //封装请求参数param
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", mobile);
        querys.put("param", "**code**:"+code+",**minute**:10");
        querys.put("smsSignId", sign);
        querys.put("templateId", template);

        Map<String, String> bodys = new HashMap<String, String>();

        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtil.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
